/**
 * 
 */
package net.looker.data;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.logging.Logger;

import net.looker.util.Html;
import net.looker.util.MysqlQuery;

/**
 * MySQL数据库驱动
 * 保存新闻至数据库
 * @author solosky
 *
 */
public class MysqlDataDriver extends DataDriver 
{	
	/**
	 * 构造函数 初始化连接
	 */
	public MysqlDataDriver()
	{
	}
	/* (non-Javadoc)
	 * @see net.looker.news.data.driver.DataDriver#add(net.looker.news.message.News)
	 */
	@Override
	public boolean add(News news) 
	{
		Logger logger = Logger.getLogger("looker");
		//建立SQL语句		
		StringBuffer sql= null;
		
		//先添加所有的附件
		Iterator<Media> it = news.getMediaList().iterator();
		Media media = null;
		while (it.hasNext())
		{
			media = it.next();
			//如果这个附件在获取文件的发生错误，不添加至数据库中
			if(media.hasError())
				continue;

			sql = new StringBuffer();
			sql.append("INSERT INTO #media");
			sql.append(" (`mediahash` ,`newshash` ,`name` , `ext`,`savedir`,`type` ,`size` ,`url` ,`title` ,`date` ,`tag` )");
			sql.append(" VALUES (");
			sql.append(""+media.getMediaHash()+", ");
			sql.append(""+news.getNewsHash()+", ");
			sql.append("'"+media.getName()+"', ");
			sql.append("'"+media.getExt()+"', ");
			sql.append("'"+media.getSaveDir()+"', ");
			sql.append("'"+media.getType().name()+"', ");
			sql.append("'"+media.getSize()+"', ");
			sql.append("'"+media.getUrl()+"', ");
			sql.append("'"+media.getTitle()+"', ");
			sql.append(media.getDate().getTime()+", ");
			sql.append("'"+media.getTag()+"'); ");
		
			try {
				MysqlQuery.getInstance().execute(sql.toString());
			} catch (SQLException e) {
				logger.warning("添加附件数据至数据库出错："+e.getMessage());
			} 
			
		}
		
		//添加新闻数据值数据库
		sql = new StringBuffer();
		sql.append("INSERT INTO #news ( ");
		sql.append("`newshash` ,`title`, `author` ,`date` ,`intro`, `brief`, `original`,");
		sql.append("`categoryhash` ,`contenthash` ,`tag` ,`url` ,`media`, `thumb`,`image`, `audio`, `video`, `doc`, `other`, `status`");
		sql.append(") VALUES (");
		sql.append(news.getNewsHash());
		sql.append(", '");
		sql.append(Html.sqlSafe(news.getTitle()));
		sql.append("', '");
		sql.append(news.getAuthor());
		sql.append("', ");
		sql.append(news.getDate().getTime()/1000);
		sql.append(", '");
		sql.append(Html.sqlSafe(news.getIntro()));
		sql.append("', '");
		sql.append(Html.sqlSafe(news.getBrief()));
		sql.append("', '");
		sql.append(Html.sqlSafe(news.getOriginal()));
		sql.append("', ");
		sql.append(news.getCategory().getCategoryHash());
		sql.append(", ");
		sql.append(news.getContentHash());
		sql.append(", '");
		sql.append(news.getTag());
		sql.append("', '");
		sql.append(news.getUrl());
		sql.append("',");
		sql.append(news.getMedia());
		sql.append(", ");
		sql.append( news.getThumb());
		sql.append(", "+news.getImage());
		sql.append(", "+news.getAudio());
		sql.append(", "+news.getVideo());
		sql.append(", "+news.getDoc());
		sql.append(", "+news.getOther());
		sql.append(", 1);");
		//System.out.println(sql.toString());
		
		try {
			MysqlQuery.getInstance().execute(sql.toString());
		} catch (SQLException e) {
			logger.warning("添加新闻数据至数据库出错："+e.getMessage());
			return false;
		} 
		
		return true;
	}

	/* (non-Javadoc)
	 * @see net.looker.news.data.driver.DataDriver#delete(net.looker.news.message.News)
	 */
	@Override
	public boolean delete(News news)
	{
		String sql = null;
		Logger logger = Logger.getLogger("looker");
		//首先删除所有的新闻附件
		sql = "DELETE FROM #media WHERE newshash="+news.getNewsHash()+"";
		try {
			MysqlQuery.getInstance().execute(sql.toString());
		} catch (SQLException e) {
			logger.warning("删除新闻附件数据出错："+e.getMessage());
		} 
		
		//接下来删除新闻
		sql = "DELETE FROM #news WHERE newshash="+news.getNewsHash()+"";
		try {
			MysqlQuery.getInstance().execute(sql.toString());
		} catch (SQLException e) {
			logger.warning("删除新闻数据出错："+e.getMessage());
			return false;
		} 
		
		return true;
	}
	/* (non-Javadoc)
	 * @see net.looker.data.DataDriver#update(net.looker.data.News)
	 */
	@Override
	public boolean update(News news) {
		// TODO Auto-generated method stub
		return false;
	}


}
